<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 通用用户方法
 *
 * @author      Eden
 * @datetime    2016/12/3 16:16
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class User_Model extends ADM_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取用户资料
     * @param $userId
     * @return array|bool
     */
    public function getUserInfo($userId)
    {
        if (empty($userId)) {
            return false;
        }

        try {
            $userDb = clone($this->db);
            $userDb->where('id', $userId);
            $userDb->join('user_profile', 'user_profile.userId = user.id');
            $query = $userDb->get('user');
            $row = $query->row_array();

            if (empty($row)) {
                return false;
            }

            $wxDb = clone($this->db);
            $level_1 = $wxDb->where(['puserid' => $row['id']])->count_all_results('user_wx_relationship');
            $level_2 = $wxDb->where(['ppuserid' => $row['id']])->count_all_results('user_wx_relationship');

            $this->load->library('core/Upload', null, 'up');
            return array(
                'userId' => intval($row['id']),
                'phone' => $row['phone'],
                'nickName' => $row['nickName'],
                'lastIp' => $row['lastIp'],
                'lastTime' => $row['lastTime'],
                'created' => $row['created'],
                'score' => intval($row['score']),
                'amount' => intval($row['amount']),
                'amount_cny' => money_format('%i', $row['amount'] / 100),
                'amountCashout' => intval($row['amountCashout']),
                'amountCashout_cny' => money_format('%i', $row['amountCashout'] / 100),
                'amountUnLimit' => intval($row['amountUnLimit']),
                'amountUnLimit_cny' => money_format('%i', $row['amountUnLimit'] / 100),
                'amountBuy' => intval($row['amountBuy']),
                'amountBuy_cny' => money_format('%i', $row['amountBuy'] / 100),
                'amountFreeze' => intval($row['amountFreeze']),
                'amountFreeze_cny' => money_format('%i', $row['amountFreeze'] / 100),
                'source' => $row['source'],
                'status' => intval($row['status']),
                'underline' => intval($level_1) + intval($level_2),
                'avatar' => thumb($row['avatar'], 100, 100),
                'trueName' => $row['trueName']
            );
        } catch(PDOException $e) {}
        return false;
    }

    /**
     * 获取地址列表
     * @param $id
     * @return array|bool
     */
    public function getAddressInfo($id)
    {
        if (empty($id)) {
            return false;
        }

        try {
            $query = $this->db->get_where('user_address', array(
                'userId' => $id,
                'status' => 1
            ));
            $rows = $query->result_array();

            $array = array();
            if (!empty($rows) && is_array($rows)) foreach ($rows as $row) {
                $array[] = array(
                    'addressId' => (int)$row['id'],
                    'name' => value($row, 'name'),
                    'phone' => value($row, 'phone'),
                    'province' => value($row, 'province'),
                    'city' => value($row, 'city'),
                    'area' => value($row, 'area'),
                    'address' => value($row, 'address'),
                    'created' => value($row, 'created'),
                    'default' => !empty($row['default'])
                );
            }

            return !empty($array) ? $array : false;

        } catch(PDOException $e) {}
        return false;
    }

    /**
     * 获取最新用户
     * @param int $limit
     * @return mixed
     */
    public function getLatestUsers($limit = 20)
    {
        $where = ['u.status' => 1];
        $adminid = adminId();
        if ($adminid == 2) {
            $where['u.virtual'] = 0;
        }

        $query = $this->db
            ->select('u.id, u.nickName, u.created, p.avatar')
            ->where($where)
            ->from('user as u')
            ->order_by('u.created', 'DESC')
            ->join('user_profile as p', 'p.userId = u.id')
            ->limit($limit)
            ->get();

        return $query->result_array();
    }

    /**
     * 删除所有用户的海报
     * @return mixed
     */
    public function cleanBill()
    {
        $db = clone($this->db);
        $db->update('user_profile', ['bill' => '']);
        return $db->affected_rows();
    }
}
